import 'package:flutter/material.dart';

typedef CustomSliverPersistentHeaderBuilder = Widget Function(BuildContext context, double shrinkOffset, bool overlapsContent);

class CustomSliverPersistentHeaderDelegate extends SliverPersistentHeaderDelegate {
  final double maxSize;
  final double minSize;
  final CustomSliverPersistentHeaderBuilder builder;

  // child 为 header
  CustomSliverPersistentHeaderDelegate({
    required this.maxSize,
    this.minSize = 0,
    required Widget child,
  })  : builder = ((a, b, c) => child),
        assert(minSize <= maxSize && minSize >= 0);

  //最大和最小高度相同
  CustomSliverPersistentHeaderDelegate.fixedSize({
    required double size,
    required Widget child,
  })  : builder = ((a, b, c) => child),
        maxSize = size,
        minSize = size;

  //需要自定义builder时使用
  CustomSliverPersistentHeaderDelegate.builder({
    required this.maxSize,
    this.minSize = 0,
    required this.builder,
  });

  @override
  double get maxExtent => maxSize;

  @override
  double get minExtent => minSize;

  @override
  Widget build(BuildContext context, double shrinkOffset, bool overlapsContent) {
    Widget child = builder(context, shrinkOffset, overlapsContent);
    return SizedBox.expand(child: child);
  }

  @override
  bool shouldRebuild(CustomSliverPersistentHeaderDelegate oldDelegate) {
    return oldDelegate.maxExtent != maxExtent || oldDelegate.minExtent != minExtent;
  }
}
